package com.kayden.book.controller;

import com.kayden.book.constant.Constants;
import com.kayden.book.model.UserInfo;
import com.kayden.book.service.UserInfoService;
import jakarta.servlet.http.HttpSession;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.util.StringUtils;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;

@RequestMapping("/user")
@RestController
public class UserController {

    @Autowired
    private UserInfoService userInfoService;

    @RequestMapping("/login")
    public Boolean login(String name, String password, HttpSession session){
        /**
         * 1.校验参数
         * 2.校验账号密码
         * 3.设置session
         * 4.返回结果
         */
        if (!StringUtils.hasLength(name) || !StringUtils.hasLength(password)){
            return false;
        }
        UserInfo userInfo = userInfoService.queryUserInfoByUserName(name);
        if(userInfo == null){
            return false;
        }
        if (password.equals(userInfo.getPassword())) {
            userInfo.setPassword("");
            session.setAttribute(Constants.SESSION_USER_KAY, userInfo);
            return true;
        }
//        if ("admin".equals(name) && "admin".equals(password)){
//            session.setAttribute("userName", name);
//            return true;
//        }
        return false;
    }

}
